﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;


public partial class MasterPage : System.Web.UI.MasterPage
{
    public long total;
    public int so_luong;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Load_ParentProduct();
            RefreshShoppingCartInfo();
            
        }
    }
    public void RefreshShoppingCartInfo()
    {
        total = 0;
        so_luong = 0;
        ShoppingCart card = (ShoppingCart)Session["gio_hang"];
        if (card != null)
        {
            so_luong = card.Count_Product;
            total = card.Total;
        }
    }

    protected void ImageButton1_Command(object sender, CommandEventArgs e)
    {
        switch (e.CommandArgument.ToString())
        {
            case "vi-VN":
                Session["lang"] = "vi-VN";
                break;
            case "en-US":
                Session["lang"] = "en-US";
                break;
        }
        Response.Redirect(Request.Url.ToString());
    }

    
    protected void SetSessionLang()
    {
        if (Request["lang"] != null)
        {
            if (Request["lang"] == "en-US")
                Session["lang"] = "en-US";
            else
                Session["lang"] = "vi-VN";
        }
        if (Session["lang"] == null)
            Session["lang"] = "vi-VN";
    }

    protected void Load_ParentProduct()
    {
        //----hien thi danh muc cap 1----
        SqlConnection mycon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringMembership"].ConnectionString);
        mycon.Open();
        string sql = "select * from LoaiTaiLieu where IDLoaiTLCha=0"; //---menu cap 1 la menu co ParentId=0
        SqlCommand myCommand = new SqlCommand(sql, mycon);
        SqlDataAdapter myAdap = new SqlDataAdapter(myCommand);
        DataSet ds = new DataSet();
        myAdap.Fill(ds);
        rpListParentProduct.DataSource = ds.Tables[0];
        rpListParentProduct.DataBind();
        myCommand.Dispose();
        mycon.Close();
    }

    protected void rpListParentProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //---tuong ung voi mot san pham A duoc dua ra, hien thi danh sach san pham con cua A
        HiddenField hrId = (HiddenField)e.Item.FindControl("hrID"); //--- day la Id cua loai san pham A---
        Repeater rpChildrent = (Repeater)e.Item.FindControl("rpListChildProduct");
        if ((hrId != null) && (rpChildrent != null))
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringMembership"].ConnectionString);
            conn.Open();
            string sql = "select * from LoaiTaiLieu where IDLoaiTLCha=" + hrId.Value.ToString(); //---menu con cua A co ParentID la Id cua A
            //string sql = "select * from ProductGroup where ParentId=0";
            SqlCommand myCommand = new SqlCommand(sql, conn);
            SqlDataAdapter myAdap = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            myAdap.Fill(ds);
            rpChildrent.DataSource = ds.Tables[0];
            rpChildrent.DataBind();
            myCommand.Dispose();
            conn.Close();
        }
    }
    protected void find_Click(object sender, ImageClickEventArgs e)
    {
        if (key.Text.ToString() == "")
        {
            return;
        }
        string url = "../TimKiem.aspx?key=" + key.Text.ToString() + "&loai=" + DropDownList1.SelectedValue.ToString();
        Response.Redirect(url);
    }


    public string format_tt(string tt)
    {

        string rs = tt;
        //thay dau ',' thanh '.'
        int l = (rs.Length - 1) / 3;
        for (int i = 0; i < l; i++)
        {
            rs = rs.Insert(rs.Length - (i + 1) * 3 - i, ".");
        }

        return rs;
    }
}
